package xzy.Controller;

import lombok.SneakyThrows;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xzy.dao.StudentMesMapper;
import xzy.pojo.PageBean;
import xzy.pojo.Student;
import xzy.pojo.StudentMes;
import xzy.util.JsonUtil;
import xzy.util.ResponseUtil;
import xzy.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

@WebServlet(name = "StudentList_Servlet")
public class StudentList_Servlet extends HttpServlet {
    @Autowired
    SqlSessionTemplate sqlSessionTemplate;
    ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    StudentMesMapper studentMesMapper= (StudentMesMapper) context.getBean("studentMesMapper");
    @SneakyThrows
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入学生信息分页查询");
        String page=request.getParameter("page");
        String rows=request.getParameter("rows");
        String stuNo=request.getParameter("stuNo");
        String stuName=request.getParameter("stuName");
        String sex=request.getParameter("sex");
        String bbirthday=request.getParameter("bbirthday");
        String ebirthday=request.getParameter("ebirthday");
        String gradeId=request.getParameter("gradeId");
        StudentMes studentMes=new StudentMes();
        if(stuNo!=null){
            studentMes.setStuNo(stuNo);
            studentMes.setStuName(stuName);
            studentMes.setSex(sex);
            if(StringUtil.isNotEmpty(gradeId)){
                studentMes.setGradeId(Integer.parseInt(gradeId));
            }
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
            JSONObject result=new JSONObject();
            JSONArray jsonArray= JsonUtil.formatRsToJsonArray(studentMesMapper.getStudentMesAll(pageBean,studentMes,bbirthday,ebirthday));
            int total=studentMesMapper.studentCount(studentMes,bbirthday,ebirthday);
            result.put("rows", jsonArray);
            result.put("total", total);
            ResponseUtil.write(response, result);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
